#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<queue>

using namespace std;

typedef pair<int, int> PII;
int dx[4] = { 0,0,1,-1 };
int dy[4] = { 1,-1,0,0 };

class Solution {
public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int n = grid.size(), m = grid[0].size();
        vector<vector<bool>> st(n, vector<bool>(m));
        queue<PII> q;
        int res = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
            {
                if (grid[i][j] == 0 || st[i][j]) continue;
                q.push({ i,j });
                st[i][j] = true;
                int cnt = 0;
                while (q.size())
                {
                    auto t = q.front();
                    q.pop();
                    cnt++;
                    for (int i = 0; i < 4; i++)
                    {
                        int a = t.first + dx[i], b = t.second + dy[i];
                        if (a < 0 || b < 0 || a >= n || b >= m)  continue;
                        if (grid[a][b] != 1) continue;
                        if (st[a][b])    continue;
                        st[a][b] = true;
                        q.push({ a,b });
                    }
                }
                res = max(res, cnt);
            }
        return res;
    }
};